package com.fun.feifun.base.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@Component
public class LogInterceptor implements HandlerInterceptor {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        logger.info("请求方式："+request.getMethod()+"请求路径{}", request.getRequestURI());
        // 获取所有请求参数的名称
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            // 获取参数名
            String paramName = paramNames.nextElement();
            // 获取参数值
            String[] paramValues = request.getParameterValues(paramName);
            // 打印参数名和参数值
            System.out.print(paramName + ": ");
            if (paramValues.length == 1) {
                System.out.println(paramValues[0]);
            } else {
                System.out.println(java.util.Arrays.toString(paramValues));
            }
        }
        return true;
    }
}
